package com.example.qxfw.shfx.Controller;

import com.example.qxfw.common.SysResult;
import com.example.qxfw.shfx.Service.YjybService;
import com.example.qxfw.shfx.dto.YjqDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

@Api(tags = "山洪防汛-预警预报")
@CrossOrigin
@RestController
@RequestMapping("/EarlyWarningAndForecastingController")
public class EarlyWarningAndForecastingController {

    @Value("${filePath.active}")
    private String active;

    @Autowired
    private YjybService yjybService;

    @ApiOperation("山洪防汛-预警预报-获取预警区内乡镇数据")
    @PostMapping("/getYjqCountry")
    public SysResult getYjqCountry(@RequestBody List<YjqDto> yjqDtoList) throws UnsupportedEncodingException {
        Map<String, String> country = yjybService.getCountry(yjqDtoList);
        return SysResult.success(country.keySet(), country.values());
    }

    @ApiOperation("模板下载")
    @GetMapping("/getTemplate")
    public Object gettTemplate() {
        //模板路径
        String templatePath;
        ApplicationHome applicationHome = new ApplicationHome(this.getClass());
        if (active.equals("dev")) {
            templatePath = applicationHome.getDir().getParentFile().getParentFile().getAbsolutePath() +
                    "\\src\\main\\resources\\Documents\\风险预警模板\\山洪灾害气象风险预警模板.docx";
        } else {
            templatePath = applicationHome.getSource().getParentFile() +
                    "\\resource\\Documents\\风险预警模板\\山洪灾害气象风险预警模板.docx";
        }

        // 设置前端响应
        byte[] buffer = new byte[1024];
        int bytesRead;
        try (
                ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
                FileInputStream inputStream = new FileInputStream(templatePath);
        ) {
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, bytesRead);
            }
            byte[] byteArray = byteArrayOutputStream.toByteArray();
            HttpHeaders headers = new HttpHeaders();
            headers.setContentDispositionFormData("attachment", "山洪预警文档模板.docx");
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentLength(byteArray.length);
            return new ResponseEntity<>(byteArray, headers, HttpStatus.OK);
        } catch (Exception e) {
            System.err.println("模板下载失败");
        }
        return null;
    }

    @ApiOperation("获取产品最新期数")
    @GetMapping("getMaxId")
    public SysResult getMaxId(){
        Integer maxId = yjybService.getMaxId();
        return SysResult.success(maxId);
    }

    @ApiOperation("生成产品")
    @PostMapping("/createProduct")
    public Object createProduct(
                                Integer issueNum,
                                String userName,
                                String yjType,
                                String sXType,
                                String date,
                                String timeChar,
                                Integer nTimes,
                                String colorAndPos,
                                String posStr,
                                MultipartFile[] images,
                                String layerName,
                                String dlData) throws IOException, NoSuchFieldException, IllegalAccessException, ParseException {
        return yjybService.createProduct(issueNum, userName, yjType, sXType, date, timeChar, nTimes, colorAndPos, posStr, images, layerName, dlData);
    }


}
